// xc_lshop/club/manage/pro/com/import.js
var actbillclass = require('../../../../../xc/model/pro_class')
var actbill = require('../../../../../xc/model/pro_contents')
var actbillclub = require('../../../model/clubshop_pro')

const app = getApp()
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    themcolor: {
      type: Object,
      observer: function (newval, oldval) {
       
      },
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchkey:"",
    isshow: false,
    bpid:"0",
    bindex:"0",
    spid:"0",
    CustomBar: app.globalData.CustomBar,
    modellist:[],
    allchecked:false,
    platformproids:[],
    onlychecked:true
  },

  /**
   * 组件的方法列表
   */
  ready(){
    var  that=this;
    
  },
  methods: {
    show() {
      // this.getclass()
      this.setData({
        isshow: true
      })
      this.getclass()
    this.getpagelist(that, 1)
    this.getproids()
    },
    exit(){
      this.setData({
        isshow: false
      })
    },
    onbpidChange(e){
      var that=this;
      this.setData({
        allchecked:false,
        bpid:e.detail.name,
        bindex:e.detail.index,
        spid:"0",
        sindex:"0"
      })
      that.getpagelist(that,1)
    },
    onspidChange(e){
      var that=this;
      var idx=e.detail;
      if(idx!=0){
        var spid=this.data.treeclass.tree[this.data.bindex-1].children[idx - 1]['id'];
      }else{
        var spid="0"
      }
      this.setData({
        allchecked:false,
        spid:spid,
        sindex:idx
      })
      that.getpagelist(that,1)
    },
    //加载全部分类
    getclass() {
      var that = this
      actbillclass.apiall().then(item => {
        
        if(item.status==1){
          that.setData({
            treeclass: item.obj,
          })
        }
      })
    },
    //加载产品列表
    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        if (!strwhere) {
          strwhere = {};
        }
  
        strwhere["status"] = 1;
        strwhere["bpid"] = this.data.bpid;
        strwhere["spid"] = this.data.spid;
        strwhere["seachval"] = this.data.searchkey;
     
        that.setData({ modellist: [] });
      }
      actbill.offset(that, rest, strwhere).then(function (result) {
        var updatemdoel = {};
  
        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
  
           for(var a in result["obj"]["modellist"] ){
             if(result["obj"]["modellist"][a]){
              result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
             }
           }     
          if (rest == 1) {
            updatemdoel["modellist"] = result["obj"]["modellist"];
          }
          else {
            if (result["obj"]["modellist"].length > 0) {
              updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
            }
          }
        }
        wx.stopPullDownRefresh();
        that.setData(updatemdoel);
       
      });
    },
    //选择产品
    choosepro(e){
      var dataset=e.currentTarget.dataset;
      var index=dataset.index;
      var name='modellist['+index+'].checked';
      var val=this.data.modellist[index].checked;
      //  if(empty(val)){
      //   val=false
      //  }
      this.setData({
          [name]:!val
      })
    },
    onreachbottom(){
      var that = this
      that.getpagelist(that, -1)
    },
    batchoperation(ev) {
      var that = this;
      var dataset = ev.currentTarget.dataset
  
      var ids = [];
      this.data.modellist.forEach(item => {
        if (item.checked) {
          ids.push(item.id)
        }
  
      })
      if (ids.length < 1) {
        wx.showModal({
          title: "请选择要操作的产品",
          content: "请选择要操作的产品",
          showCancel: false
  
        })
      } else {
        switch (dataset.op) {
          case "importpro": {
         
            actbillclub.importpro(ids.join(",")).then(function (result) {
             
              if (result["status"] == 1) {
                var updatemodel = {};
                that.data.modellist.forEach((item, index) => {
                  if (item.checked) {
                    updatemodel['modellist[' + index + '].checked'] = false;
                  }
                })
                that.setData(updatemodel)
                that.getproids()
                wx.showToast({
                  title: '成功导入' + result.obj + "个",
                })
              } else {
                wx.showToast({
                  title: '操作失败',
                })
              }
            })
            break;
          }
  
        }
      }
  
  
    },
    onChangeAll(e){
      var that=this;
      var checked=that.data.allchecked;
      var modellist=that.data.modellist;
      for(var i in modellist){
        modellist[i]['checked']=!checked;
      }
      this.setData({
        modellist:modellist,
        allchecked:!checked
      })
    },
    //获取平台已导入产品
    getproids(){
      var that = this
      actbillclub.getproids().then(res => {
         if(res['status']==1){
           this.setData({
            platformproids:res['obj']
           })
         }
      })
    },
    ononlyChange(){
      this.setData({
        onlychecked:!this.data.onlychecked
      })
    }
  }
})